from flask import Flask, request, jsonify, Blueprint
from app.services.SoapOperaService import SoapOperaService
from app.response import ResponseBuilder

perfix = "/soapOpera"

soapOpera_bp = Blueprint("soapOpera_bp",__name__, url_prefix=perfix)

@soapOpera_bp.route("/create_soap_opera", methods=["POST"])
def create_soap_opera():
    """Create soap opera"""
    title = request.json.get("title")
    description = request.json.get("description")
    type = request.json.get("type")
    director = request.json.get("director")
    year = request.json.get("year")
    rating = request.json.get("rating")
    cover = request.json.get("cover")
    episodes = request.json.get("episodes")

    soapOperaService = SoapOperaService()
    soapOperaService.create_soap_opera(title=title, description=description, type=type, director=director, year=year, rating=rating, cover=cover, episodes=episodes)
    return ResponseBuilder.success(200, "Soap Opera created successfully")

@soapOpera_bp.route("/update_soap_opera", methods=["PUT"])
def update_soap_opera():
    """Update soap opera"""
    id = request.json.get("id")
    title = request.json.get("title")
    description = request.json.get("description")
    type = request.json.get("type")
    director = request.json.get("director")
    year = request.json.get("year")
    rating = request.json.get("rating")
    cover = request.json.get("cover")
    episodes = request.json.get("episodes")
    soap_opera_service = SoapOperaService()
    soap_opera_service.update_soap_opera(id=id, title=title, description=description, type=type, director=director, year=year, rating=rating, cover=cover, episodes=episodes)
    return ResponseBuilder.success(200, "Soap Opera updated successfully")

@soapOpera_bp.route("/get_soap_opera_list", methods=["GET"])
def get_soap_opera_list():
    """Get soap opera list"""
    title = request.args.get("params[title]")
    type = request.args.get("params[type]")
    director = request.args.get("params[director]")
    year = request.args.get("params[year]")
    rating = request.args.get("params[rating]")
    page_num =  int(request.args.get("pageNum"))
    page_size = int(request.args.get("pageSize"))
    soap_opera_service = SoapOperaService()
    soap_operas = soap_opera_service.get_soap_opera_list(title=title, type=type, director=director, year=year, rating=rating, page_num=page_num, page_size=page_size)
    sopap_opera_list = [soap_opera.to_dict() for soap_opera in soap_operas]

    total= soap_opera_service.get_soap_opera_total(title=title, type=type, director=director, year=year, rating=rating)
    return ResponseBuilder.success({"list":sopap_opera_list, "total":total}, "Soap Opera list retrieved successfully", )
        
@soapOpera_bp.route("/get_soap_opera_by_id", methods=["GET"])
def get_soap_opera_by_id():
    """Get soap opera by id"""
    id = request.args.get("id")
    soap_opera_service = SoapOperaService()
    soap_opera = soap_opera_service.get_soap_opera_by_id(id=id)
    return ResponseBuilder.success(200, "Soap Opera retrieved successfully", soap_opera.to_dict())

@soapOpera_bp.route("/delete_soap_opera", methods=["DELETE"])
def remove_soap_opera():
    """Remove soap opera"""
    ids = request.json.get("ids")
    soap_opera_service = SoapOperaService()
    soap_opera_service.delete_soap_opera(ids=ids)
    return ResponseBuilder.success(200, "Soap Opera removed successfully")

    
